package com.ewanghuiju.app.presenter.login;

import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.login.PhoneRegistbindContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.request.PhoneRegistbindRequestBean;
import com.ewanghuiju.app.model.bean.response.PhoneCodeResponseBean;
import com.ewanghuiju.app.model.bean.response.PhoneRegistbindResponBean;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/14
 */
public class PhoneRegistbindPresenter extends RxPresenter<PhoneRegistbindContract.View> implements PhoneRegistbindContract.Presenter {

    private DataManager mDataManager;
    private Disposable intervalSubscription;

    @Inject
    public PhoneRegistbindPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void phoneRegist(PhoneRegistbindRequestBean phoneRegistbindRequestBean) {
        addSubscribe(mDataManager.phoneRegist(phoneRegistbindRequestBean)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<PhoneRegistbindResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<PhoneRegistbindResponBean> phoneRegistbindResponBean) {
                                mView.showRegist(phoneRegistbindResponBean.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                if (e instanceof ApiException) {
//                                    mView.showRegistResponseMsg(((ApiException) e).getCode(), e.getMessage());
//                                }
                            }
                        })
        );
    }

    @Override
    public void Editpwd(PhoneRegistbindRequestBean phoneRegistbindRequestBean) {
        //修改密码
        addSubscribe(mDataManager.userEditpwd(phoneRegistbindRequestBean)
                        .compose(RxUtil.<MyHttpResponse<PhoneRegistbindResponBean>>rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<PhoneRegistbindResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<PhoneRegistbindResponBean> phoneRegistbindResponBean) {
                                mView.showRegist(phoneRegistbindResponBean.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                if (e instanceof ApiException) {
//                                    mView.showRegistResponseMsg(((ApiException) e).getCode(), e.getMessage());
//                                }
                            }
                        })
        );
    }

    @Override
    public void phoneBind(PhoneRegistbindRequestBean phoneRegistbindRequestBean) {

    }

    @Override
    public void getCode(String mobile) {
        //获取验证码
        addSubscribe(mDataManager.getCode(mobile)
                .compose(RxUtil.<MyHttpResponse<PhoneCodeResponseBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PhoneCodeResponseBean>>(mView) {
                    @Override
                    public void onNext(Optional<PhoneCodeResponseBean> optional) {
                        StyleableToast.makeText(App.getInstance(), "获取验证码成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        startInterval();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getCodeWithReg(String mobile, String type) {

        addSubscribe(mDataManager.getCodeWithReg(mobile, type)
                .compose(RxUtil.<MyHttpResponse<PhoneCodeResponseBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<PhoneCodeResponseBean>>(mView) {
                    @Override
                    public void onNext(Optional<PhoneCodeResponseBean> optional) {
                        StyleableToast.makeText(App.getInstance(), "获取验证码成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        startInterval();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    @Override
    public void startInterval() {
        final int count = 60;

        if (intervalSubscription != null && !intervalSubscription.isDisposed()) {
            return;
        }
        intervalSubscription = Flowable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)//设置循环次数
                .onBackpressureDrop()
                .compose(RxUtil.<Long>rxSchedulerHelper())
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                }).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (mView == null)
                            return;
                        if (aLong >= count) {
                            mView.showCodeOn();
                            mView.showCodeText("重新获取(" + aLong + "s)");
                        } else if (aLong <= 0) {
                            mView.showCodeComplete();
                        } else {
                            mView.showCodeText("重新获取(" + aLong + "s)");
                        }
                    }
                });
        addSubscribe(intervalSubscription);
    }

    @Override
    public void stopInterval() {

        if (intervalSubscription != null && !intervalSubscription.isDisposed()) {
            intervalSubscription.dispose();
        }

    }

//    public void countDown() {
//        Observable mObservable = Observable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟，每隔一秒发送一条数据
//                .take(count + 1)//设置循环次数
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .map(new Function<Long, Long>() {
//                    @Override
//                    public Long apply(Long aLong) throws Exception {
//                        return count - aLong;
//                    }
//                });

//        Observer observer = new Observer<Long>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(Long aLong) {//接受到一条就会操作一次UI
//                mView.showCodeText("重新获取(" + aLong + "s)");
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onComplete() {
//                mView.showCodeComplete();
//            }
//        };
//        mObservable.doOnSubscribe(new Consumer<Disposable>() {
//            @Override
//            public void accept(Disposable disposable) throws Exception {
//
//                mView.showCodeOn();
//
//            }
//        }).subscribe(observer);
//    }
}
